﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QL_DTTN.DAL;
namespace QL_DTTN.DTO.DTO
{
    public partial class DiemThi:DtoClass
    {
        public DataTable dataTable(int maTinh)
        {
            string sql = " select * from " + _tableName + " where MaTinh=@pMaTinh ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pMaTinh", maTinh));
            return dp.ExDataTable(sql, true);
        }
        public DiemThi GetDiemThi(long maDiemThi)
        {
            bool kt = false;
            SqlDataProvider dp = new SqlDataProvider();
            System.Data.SqlClient.SqlDataReader sqlRr = GetObject(maDiemThi, dp);
            DiemThi dt = new DiemThi();
            dt.MaDiemThi = maDiemThi;
            if (sqlRr != null)
            {
                while (sqlRr.Read())
                {     
                    if (!Convert.IsDBNull(sqlRr["tenDiemThi"])) dt.TenDiemThi = sqlRr["tenDiemThi"].ToString();
                    if (!Convert.IsDBNull(sqlRr["soDienThoai"])) dt.SoDienThoai = sqlRr["soDienThoai"].ToString();
                    if (!Convert.IsDBNull(sqlRr["maTinh"])) dt.MaTinh = Convert.ToInt16(sqlRr["maTinh"]);
                    if (!Convert.IsDBNull(sqlRr["diaChi"])) dt.DiaChi = sqlRr["diaChi"].ToString();
                    kt = true;
                }
                dp.CloseConnection();
                if (kt) return dt;
                else return null;
            }
            else
            {
                return null;
            }
        }
        public bool Them(DiemThi diemThi,List<PhongThi>lstPhong)
        {
            string strNew = " insert into " + _tableName + " (TenDiemThi,DiaChi,SoDienThoai,MaTinh) values (@pTenDiemThi,@pDiaChi,@pSoDienThoai,@pMaTinh) select @@IDENTITY ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pTenDiemThi", diemThi.TenDiemThi));
            dp.AddParameter(new SqlParameter("@pDiaChi", diemThi.DiaChi));
            dp.AddParameter(new SqlParameter("@pSoDienThoai", diemThi.SoDienThoai));
            dp.AddParameter(new SqlParameter("@pMaTinh", diemThi.MaTinh));
            List<string> lstStr = new List<string>();
            for (int i = 0; i < lstPhong.Count(); i++)
            {
                string subStr = " insert into PhongThi (MaDiemThi,TenPhongThi) values (@pMaDiemThi,@pTenPhongThi" + i + ") ";
                dp.AddParameter(new SqlParameter("@pTenPhongThi" + i, lstPhong[i].TenPhongThi));
                lstStr.Add(subStr);
            }
            return dp.RunAddNewIdentity(strNew, lstStr, "@pMaDiemThi", true);
        }
        public bool Sua(DiemThi diemThi)
        {
            string str = " update " + _tableName + " set TenDiemThi= @pTenDiemThi, DiaChi= @pDiaChi, SoDienThoai = @pSoDienThoai, MaTinh= @pMaTinh where MaDiemThi = @pMaDiemThi";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pMaDiemThi", diemThi.MaDiemThi));
            dp.AddParameter(new SqlParameter("@pTenDiemThi", diemThi.TenDiemThi));
            dp.AddParameter(new SqlParameter("@pDiaChi", diemThi.DiaChi));
            dp.AddParameter(new SqlParameter("@pSoDienThoai", diemThi.SoDienThoai));
            dp.AddParameter(new SqlParameter("@pMaTinh", diemThi.MaTinh));
            return dp.RunSQL(str, true);
        }
        public bool Them(PhongThi phongThi)
        {
            string str = " insert into PhongThi (MaDiemThi,TenPhongThi) values (@pMaDiemThi,@pTenPhongThi) ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pTenPhongThi", phongThi.TenPhongThi));
            dp.AddParameter(new SqlParameter("@pMaDiemThi", phongThi.MaDiemThi));
            return dp.RunSQL(str,true);
        }
    }
}
